package edu.pku.sei.transformation.editor.controller.editparts.link;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;

import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.deploy.ActualParameter;
import edu.pku.sei.transformation.editor.model.deploy.Deployment;
import edu.pku.sei.transformation.editor.views.figures.ConnectionFigure;

public class DeploymentEditpart extends TransformationLinkEditPart {

	public DeploymentEditpart(MetaShapeElement conn) {
		super(conn);
	}
	
	@Override
	protected IFigure createFigure() {
		ConnectionFigure figure = new ConnectionFigure();
		figure.setLineStyle(SWT.LINE_DASH);
		figure.setTargetDecoration(ConnectionFigure.ARROW);
		figure.setLabel("<<deploy>>", ConnectionFigure.CONNECTION_FIGURE_MIDDLE);
		refreshToolTip(figure);
		return figure;
	}
	
	public void refreshToolTip(Figure f) {
		Figure figure = new Figure();
		Deployment d = getDeployment();
		
		figure.setLayoutManager(new ToolbarLayout());
		String direction;
		if (d.getDirection()!=null)
			direction = d.getDirection().getName();
		else
			direction = null;
		figure.add(new Label("direction:"+direction));
		figure.add(new Label("mode:"+d.getModeString()));
		for (Object obj : d.getActualParameters()) {
			ActualParameter ap = (ActualParameter) obj;
			String formalParamter;
			if (ap.getFormalParameter()!=null)
				formalParamter = ap.getFormalParameter().getName();
			else
				formalParamter = "null";
			figure.add(new Label(ap.getFormalParameter().toString()+":"+ap.getFile()));
		}
		f.setToolTip(figure);
		
	}
	
	private Deployment getDeployment() {
		return (Deployment) ((MetaLink)getModel()).getModelElement();
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		if (event.getPropertyName().equals("direction")
				|| event.getPropertyName().equals("mode"))
			refreshToolTip((Figure) getFigure());
		super.handlePropertyChanged(event);
	}

}
